// import cookie from '../cookien'
import store from '../store'
// 路由总文件
import VueRouter from 'vue-router'
import Login from '../components/Login'
import GoodsDetails from '../components/GoodsDetails'
import BackgroundHomePage from '../components/BackgroundHomePage'
import ProductsOverview from '../components/ProductsOverview'
import ThisYear from '../components/productsOverview/ThisYear'
import HundredDay from '../components/productsOverview/HundredDay'
import OrderStatistics from '../components/OrderStatistics'
import UserManagement from '../components/UserManagement'
import HeadTop from '../components/back/HeadTop.vue'
import SearchPage from '../components/font/Search/SearchPage.vue'
import Choice from '../components/font/Search/Choice.vue'
import SearchMenu from '../components/font/Search/SearchMenu.vue'
import UserSearch from '../components/font/Search/UserSearch.vue'
import ConsiderAll from '../components/font/Search/SearchRecipe/ConsiderAll.vue'
import CollectMost from '../components/font/Search/SearchRecipe/CollectMost.vue'
import LookThroughMost from '../components/font/Search/SearchRecipe/LookThroughMost.vue'
import HomePage from '../components/font/HomePage.vue'
import Home from '../components/font/home/Home.vue'
import UpFood from '../components/font/home/UpFood.vue'
import Create from '../components/font/home/Create.vue'
import Profile from '../components/font/home/userInfo/Profile.vue'
import Recipe from '../components/Recipe'
import Choiceness from '../components/Recipe/Choiceness'
import Classify from '../components/Recipe/Classify'
import Menu from '../components/Recipe/Menu'
import Newest from '../components/Recipe/Newest'
import Heat from '../components/Recipe/Menu/Heat'
import News from '../components/Recipe/Menu/News'
// import HeadNav from '../components/font/HeadNav'
import ShoppingCenter from '../components/font/ShoppingCenter'
import MenuListDetails from '../components/font/MenuListDetails'
import DishDetails from '../components/font/DishDetails'
import GoodsSituation from '../components/back/GoodsSituation'
import AddGoods from '../components/back/AddGoods'
import GoodsList from '../components/back/GoodsList'
import GoodsSummary from '../components/back/GoodsSummary'
import Sign from '../components/Sign'
import China from '../components/China/China'
import Area from '../components/Area.vue'
import OrderManagement from '../components/OrderManagement'
import test from '../components/test'

const router = new VueRouter({
  // linkActiveClass: 'is-active',
  // mode: 'history',
  linkActiveClass: 'is-active',
  routes: [
    // 前台首页
    {
      path: '/font',
      name: 'Font',
      component: HomePage,
      meta: {
        title: '首页'
      },
      // 首页重定向
      children: [
        {
          path: '/',
          redirect: 'Home'
        },
        {
          path: '/home',
          name: 'Home',
          component: Home,
          meta: {
            title: '首页'
          }
        },
        // 前台食谱
        {
          path: '/Recipe',
          name: 'Recipe',
          component: Recipe,
          meta: {
            title: '食谱'
          },
          children: [
            // 食谱重定向
            {
              path: '/',
              redirect: Choiceness
            },
            {
              path: 'Choiceness',
              name: 'Choiceness',
              component: Choiceness,
              meta: {
                title: '精选菜谱'
              }
            },
            {
              path: 'Classify',
              name: 'Classify',
              component: Classify,
              meta: {
                title: '分类'
              }
            },
            {
              path: 'Menu',
              name: 'Menu',
              component: Menu,
              meta: {
                title: '菜单'
              },
              children: [
                {
                  path: '/Recipe/Menu/',
                  redirect: '/Recipe/Menu/Heat'
                },
                {
                  path: 'Heat',
                  name: 'Heat',
                  component: Heat,
                  meta: {
                    title: '菜单-最热'
                  }
                },
                {
                  path: 'News',
                  name: 'News',
                  component: News,
                  meta: {
                    title: '菜单-最新'
                  }
                }
              ]
            },
            {
              path: 'Newest',
              name: 'Newest',
              component: Newest,
              meta: {
                title: '最新'
              }
            }
          ]
        },
        // 前台商城
        {
          path: '/ShoppingCenter',
          name: 'ShoppingCenter',
          component: ShoppingCenter,
          meta: {
            title: '前台商城'
          }
        },
        // 商品详情
        {
          path: '/goodsDetails/:id',
          name: 'GoodsDetails',
          component: GoodsDetails,
          meta: {
            title: '商品详情'
          }
        },
        // 发布菜谱
        {
          name: 'UpFood',
          path: '/UpFood',
          component: UpFood,
          meta: {
            title: '发布菜谱',
            isGuard: true
          }
        },
        // 创建菜单
        {
          name: 'Create',
          path: '/Create',
          component: Create,
          meta: {
            title: '创建菜单',
            isGuard: true
          }
        },
        {
          path: '/MenuListDetails/:id',
          name: 'MenuListDetails',
          component: MenuListDetails,
          meta: {
            title: '菜单详情页'
          }
        },
        {
          path: '/DishDetails/:id',
          name: 'DishDetails',
          component: DishDetails,
          meta: {
            title: '菜品详情页'
          }
        },
        // 前台搜索页
        {
          path: '/Search',
          name: 'Search',
          component: Choice,
          meta: {
            title: '搜索'
          },
          children: [
            // 搜索页面重定向
            // {
            //   path: '/',
            //   redirect: ConsiderAll
            // },
            {
              name: 'SearchMenu',
              path: 'SearchMenu',
              component: SearchMenu,
              meta: {
                title: '菜单搜索页'
              }
            },
            {
              name: 'UserSearch',
              path: 'UserSearch',
              component: UserSearch,
              meta: {
                title: '用户搜索页'
              }
            },
            {
              name: 'ConsiderAll',
              path: 'ConsiderAll',
              component: ConsiderAll,
              redirect: { name: 'SearchPage' },
              meta: {
                title: '菜谱搜索页'
              },
              children: [{
                path: '/',
                redirect: 'SearchPage'
              },
              {
                name: 'SearchPage',
                path: 'SearchPage',
                component: SearchPage,
                meta: {
                  title: '综合最佳'
                }
              },
              {
                name: 'LookThroughMost',
                path: 'LookThroughMost',
                component: LookThroughMost
              },
              // 食谱收藏最多
              {
                name: 'CollectMost',
                path: 'CollectMost/',
                component: CollectMost
              }
              ]
            }

          ]
        },
        // 个人中心
        {
          name: 'centerPage',
          path: '/centerPage',
          component: () => import('@/components/font/personCenter/Center'),
          meta: {
            title: '个人中心',
            isGuard: true
          },
          children: [
            // 个人中心重定向
            {
              path: '/',
              redirect: () => import('@/components/font/personCenter/CenterCollect')
            },
            {
              name: 'CenterSurvey',
              path: 'CenterSurvey/:userId',
              component: () => import('@/components/font/personCenter/CenterSurvey'),
              meta: {
                title: '概述',
                isGuard: true
              }
            },
            {
              name: 'CenterRecipes',
              path: 'CenterRecipes/:userId',
              component: () => import('@/components/font/personCenter/CenterRecipes'),
              meta: {
                title: '菜谱',
                isGuard: true
              }
            },
            {
              name: 'CenterBill',
              path: 'CenterBill/:userId',
              component: () => import('@/components/font/personCenter/CenterBill'),
              meta: {
                title: '菜单',
                isGuard: true
              }
            },
            {
              name: 'CenterCollect',
              path: 'CenterCollect',
              component: () => import('@/components/font/personCenter/CenterCollect'),
              meta: {
                title: '收藏',
                isGuard: true
              },
              children: [
                {
                  path: '/',
                  redirect: 'CollectRecipes/:userId'
                },
                {
                  path: 'CollectBill/:userId',
                  component: () => import('@/components/font/personCenter/centerCollect/CollectBill'),
                  meta: {
                    title: '收藏的菜单',
                    isGuard: true
                  }
                }, {
                  path: 'CollectRecipes/:userId',
                  name: 'CollectRecipes',
                  component: () => import('@/components/font/personCenter/centerCollect/CollectRecipes'),
                  meta: {
                    title: '收藏的菜谱',
                    isGuard: true
                  }
                }
              ]
            },
            {
              name: 'CenterFollow',
              path: 'CenterFollow/:userId',
              component: () => import('@/components/font/personCenter/CenterFollow'),
              meta: {
                title: '关注',
                isGuard: true
              }
            },
            {
              name: 'CenterFollower',
              path: 'CenterFollower/:userId',
              component: () => import('@/components/font/personCenter/CenterFollower'),
              meta: {
                title: '粉丝',
                isGuard: true
              }
            }
          ]
        },
        {
          name: 'MyNews',
          path: '/MyNews',
          component: () => import('@/components/font/myNews/News'),
          meta: {
            title: '消息提醒',
            isGuard: true
          },
          children: [
            {
              name: 'NewsFollow',
              path: 'NewsFollow',
              component: () => import('@/components/font/myNews/NewsFollow'),
              meta: {
                title: '关注提醒',
                isGuard: true,
                id: '3'
              }
            },
            {
              name: 'NewsCustom',
              path: 'NewsCustom',
              component: () => import('@/components/font/myNews/NewsCustom'),
              meta: {
                title: '官方通知',
                isGuard: true,
                id: '4'
              }
            },
            {
              name: 'NewsLike',
              path: 'NewsLike',
              component: () => import('@/components/font/myNews/NewsLike'),
              meta: {
                title: '收藏及喜欢',
                isGuard: true,
                id: '2'
              }
            },
            {
              name: 'NewsFeedback',
              path: 'NewsFeedback',
              component: () => import('@/components/font/myNews/NewsFeedback'),
              meta: {
                title: '反馈',
                isGuard: true,
                id: '0'
              }
            },
            {
              name: 'NewsWord',
              path: 'NewsWord',
              component: () => import('@/components/font/myNews/NewsWord'),
              meta: {
                title: '留言',
                isGuard: true,
                id: '1'
              }
            }
          ]
        },
        {
          name: 'Profile',
          path: '/Profile',
          component: Profile,
          meta: {
            title: '个人简介',
            isGuard: true
          }
        },
        {
          name: 'MyOrder',
          path: '/MyOrder',
          component: () => import('@/components/font/order/MyOrder'),
          meta: {
            title: '我的订单',
            isGuard: true
          }
        },
        {
          name: 'OrderAdress',
          path: '/OrderAdress',
          component: () => import('@/components/font/order/OrderAdress'),
          meta: {
            title: '收获地址',
            isGuard: true
          }
        }]
    },
    // 后台
    {
      name: 'HeadTop',
      path: '/HeadTop',
      component: HeadTop,
      meta: {
        title: '后台',
        isGuard: true,
        admin: true
      },
      children: [
        {
          path: 'BackgroundHomePage',
          name: 'BackgroundHomePage',
          component: BackgroundHomePage,
          meta: {
            title: '后台首页',
            isGuard: true,
            admin: true
          }
        },
        {
          path: 'ProductsOverview',
          name: 'ProductsOverview',
          component: ProductsOverview,
          meta: {
            title: '商品概况',
            isGuard: true,
            admin: true
          },
          children: [
            {
              name: 'HundredDay',
              path: 'HundredDay',
              component: HundredDay,
              meta: {
                title: '商品概况-近百天',
                isGuard: true,
                admin: true
              }
            },
            {
              name: 'ThisYear',
              path: 'ThisYear',
              component: ThisYear,
              meta: {
                title: '商品概况-今年',
                isGuard: true,
                admin: true
              }
            }
          ]
        },
        {
          path: '/OrderStatistics',
          name: 'OrderStatistics',
          component: OrderStatistics,
          meta: {
            title: '订单统计',
            isGuard: true,
            admin: true
          }
        },
        {
          path: '/UserManagement',
          name: 'UserManagement',
          component: UserManagement,
          meta: {
            title: '用户管理',
            isGuard: true,
            admin: true
          }
        },
        {
          path: '/OrderManagement',
          name: 'OrderManagement',
          component: OrderManagement,
          meta: {
            title: '订单管理',
            isGuard: true,
            admin: true
          }
        },
        {
          path: 'GoodsSituation',
          name: 'GoodsSituation',
          component: GoodsSituation,
          meta: {
            title: '后台商品页',
            isGuard: true,
            admin: true
          },
          children: [{
            path: 'AddGoods',
            component: AddGoods,
            meta: {
              title: '后台添加商品',
              isGuard: true,
              admin: true
            }
          }, {
            path: 'GoodsList',
            component: GoodsList,
            meta: {
              title: '后台商品列表',
              isGuard: true,
              admin: true
            }
          }, {
            path: 'GoodsSummary',
            component: GoodsSummary,
            meta: {
              title: '后台商品概况',
              isGuard: true,
              admin: true
            }
          }
          ]
        },
        {
          path: 'BackRecipes',
          name: 'BackRecipes',
          component: () => import('@/components/back/recipes/BackRecipes'),
          meta: {
            title: '菜谱详情页',
            isGuard: true
          },
          children: [
            {
              name: 'RecipesImg',
              path: 'RecipesImg',
              component: () => import('@/components/back/recipes/RecipesImg'),
              meta: {
                title: '图片管理',
                isGuard: true
              }
            },
            {
              name: 'RecipesAdd',
              path: 'RecipesAdd',
              component: () => import('@/components/font/home/UpFood.vue'),
              meta: {
                title: '添加食谱',
                isGuard: true
              }
            },
            {
              name: 'RecipesList',
              path: 'RecipesList',
              component: () => import('@/components/back/recipes/RecipesList'),
              meta: {
                title: '食谱列表',
                isGuard: true
              }
            }
          ]
        },
        {
          path: 'BackMenu',
          name: 'BackMenu',
          component: () => import('@/components/back/menu/BackMenu'),
          meta: {
            title: '菜单详情页',
            isGuard: true
          },
          children: [
            {
              name: 'MenuAdd',
              path: 'MenuAdd',
              component: () => import('@/components/font/home/Create.vue'),
              meta: {
                title: '创建菜单',
                isGuard: true
              }
            },
            {
              name: 'MenuList',
              path: 'MenuList',
              component: () => import('@/components/back/menu/MenuList'),
              meta: {
                title: '菜单列表',
                isGuard: true
              }
            }
          ]
        },
        {
          path: 'BackSystem',
          name: 'BackSystem',
          component: () => import('@/components/back/system/BackSystem'),
          meta: {
            title: '系统管理',
            isGuard: true
          },
          children: [
            {
              name: 'SystemBackground',
              path: 'SystemBackground',
              component: () => import('@/components/back/system/SystemBackground.vue'),
              meta: {
                title: '首页背景',
                isGuard: true
              }
            }]
        }
      ]
    },
    // 测试
    {
      name: 'Area',
      path: '/Area',
      component: Area
    },
    {
      name: 'China',
      path: '/China',
      component: China
    },
    // 临时测试用(font及其子路由)
    // 首页导航条
    // {
    //   path: '/HeadNav',
    //   name: 'HeadNav',
    //   component: HeadNav,
    //   meta: {
    //     title: '前台头部导航条'
    //   },
    //   children: [
    //     {
    //       name: 'recipes',
    //       path: 'recipes',
    //       component: BackgroundHomePage,
    //       meta: {
    //         title: '食谱'
    //       }
    //     },
    //     // 个人中心
    //     {
    //       name: 'centerPage',
    //       path: 'centerPage',
    //       component: () => import('@/components/font/personCenter/Center'),
    //       meta: {
    //         title: '个人中心',
    //         isGuard: true
    //       },
    //       children: [
    //         // {
    //         //   name: 'CenterSurvey',
    //         //   path: 'CenterSurvey',
    //         //   component: () => import('@/components/font/personCenter/CenterSurvey'),
    //         //   meta: {
    //         //     title: '个人中心-概述'
    //         //   }
    //         // },
    //         {
    //           name: 'CenterRecipes',
    //           path: 'CenterRecipes',
    //           component: () => import('@/components/font/personCenter/CenterRecipes'),
    //           meta: {
    //             title: '个人中心-菜谱',
    //             isGuard: true
    //           }
    //         },
    //         {
    //           name: 'CenterBill',
    //           path: 'CenterBill',
    //           component: () => import('@/components/font/personCenter/CenterBill'),
    //           meta: {
    //             title: '个人中心-菜单',
    //             isGuard: true
    //           }
    //         },
    //         {
    //           name: 'CenterCollect',
    //           path: 'CenterCollect',
    //           component: () => import('@/components/font/personCenter/CenterCollect'),
    //           meta: {
    //             title: '个人中心-收藏',
    //             isGuard: true
    //           }
    //         },
    //         {
    //           name: 'CenterFollow',
    //           path: 'CenterFollow',
    //           component: () => import('@/components/font/personCenter/CenterFollow'),
    //           meta: {
    //             title: '个人中心-关注',
    //             isGuard: true
    //           }
    //         },
    //         {
    //           name: 'CenterFollower',
    //           path: 'CenterFollower',
    //           component: () => import('@/components/font/personCenter/CenterFollower'),
    //           meta: {
    //             title: '个人中心-被关注',
    //             isGuard: true
    //           }
    //         }
    //       ]
    //     }
    //   ]
    // },
    // 测试
    {
      name: 'text',
      path: '/test',
      component: test
    },
    // 登录
    {
      path: '/Login',
      name: 'Login',
      component: Login,
      meta: {
        title: '登录'
      }
    },
    // 注册
    {
      path: '/Sign',
      name: 'Sign',
      component: Sign,
      meta: {
        title: '注册'
      }
    },
    // 忘记密码
    {
      path: '/Forget',
      name: 'Forget',
      component: () => import('@/components/Forget'),
      meta: {
        title: '忘记密码'
      }
    },
    // 前台首页重定向
    {
      path: '*',
      redirect: 'Home'
    }
  ]
}
)
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

// 全局拦截
router.beforeEach((to, from, next) => {
  // 普通用户
  console.log(to.meta.isGuard)
  if (to.meta.isGuard) {
    if (store.state.token === undefined || store.state.token === null || store.state.token === 'null') {
      console.log('---------------')
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }
  // 管理员
  if (to.meta.admin) {
    console.log(store.state.userMsg.role)
    if (store.state.token !== null && store.state.token !== 'null' && store.state.userMsg.role === '1') {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})
export default router
